package com.sun.event;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.net.Inet4Address;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@Slf4j
@Component
public class SystemStartEvent implements CommandLineRunner {

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;
    @Autowired
    private Environment environment;

    @Override
    public void run(String... args) throws Exception {
        log.info(">>>>>>>>starter publish event >>>>>>>>>>");
        EventListenParam param = new EventListenParam();
        param.setId(UUID.randomUUID().toString().substring(0,13));
        Map<String,String> map = new HashMap<>();
        map.put("server-port",environment.getProperty("server.port"));
        map.put("application-name",environment.getProperty("spring.application.name"));
        map.put("hostAddress", Inet4Address.getLocalHost().getHostAddress());
        map.put("port",environment.getProperty("server.port"));
        param.setParamMap(map);
        applicationEventPublisher.publishEvent(param);
    }
}
